/*
 * AgentPresenter.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.agents.agent;

import com.yidiandao.business.data.model.AgentModel;
import com.yidiandao.business.data.model.IAgentModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/** 代理商品Presenter */
public class AgentGoodsPresenter implements AgentGoodsContract.Presenter {

    private final IAgentModel mAgentModel;
    private AgentGoodsContract.View mView;

    public AgentGoodsPresenter(AgentGoodsContract.View agentView) {
        mView = agentView;
        mView.setPresenter(this);

        mAgentModel = new AgentModel();
    }

    @Override
    public void start() {
        getNetData(true);
    }

    @Override
    public void getNetData(final boolean isRefresh) {

        mAgentModel.loadAgentGoodList(
                isRefresh,
                mView.getPagerTag(),
                new IAgentModel.OnGetAgentListener() {

                    @Override
                    public void onSuccess(
                            List<DisplayListItem> displayListItems,
                            boolean empty,
                            boolean hasMore) {
                        if (isRefresh) {
                            mView.refreshContent(mView.checkResultObject(displayListItems));
                            mView.setRefreshComplete();
                        }
                        mView.loadMoreFinish(empty, hasMore);
                        mView.setDisplayListItems(displayListItems);
                    }

                    @Override
                    public void onFailed() {
                        if (isRefresh) {
                            mView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }
}
